<?php

namespace Parser\Separator;

use Parser\Enquiry;
use Parser\Parser;
use Parser\Quote;
use Parser\Repetition;

class DAJIA implements BaseSeparator
{
    public static $ApiKindList = [
        3000 => '030006', //车损
        4000 => '030018', //三者
        5002 => '030001', //司机
        5001 => '030009', //乘客
    ];

    public function interpreter(array $array, $parser)
    {
        if ($this->error($array, $parser)) {
            return;
        }

        $parser->result = $this->analysis($array);
    }

    /**
     *错误解析
     * Author:ls
     *
     * @param $array
     * @param Quote $parser
     * @return bool
     */
    public function error(&$array, $parser): bool
    {

        $code = $array['code'] ?? '';
        $message = $array['message'] ?? '';

        if ($code != '200' or $message != 'OK') {
            $parser->message = $message;
            $parser->code = '500';
            return true;
        }

        $array = $array['result'] ?? '';

        $exceptionCode = $array['exceptionCode']['code'] ?? '';
        $exceptionMsg = $array['exceptionCode']['msg'] ?? '';

        if ($exceptionCode != 'H503') {
            $parser->message = $exceptionMsg;
            $parser->code = '500';
            return true;
        }

        return false;
    }


    public function analysis($array): array
    {
        $enquiry = new enquiry();

        $enquiry->setTCIPremium((string)($array['compulsoryInsurance']['productInfo']['premium'] ?? '0'));
        $enquiry->setTCITotal((string)($array['compulsoryInsurance']['productInfo']['premium'] ?? '0'));

        $enquiry->setTaxTotal((string)($array['compulsoryInsurance']['carShipTax']['taxActual'] ?? '0'));
        $enquiry->setTaxPremium((string)($array['compulsoryInsurance']['carShipTax']['unitTaxAmt'] ?? '0'));

        $enquiry->setVCIDiscount($array['businessInsurance']['productInfo']['syAdjustRate'] ?? '0');

        if (isset($array['businessInsurance']['itemKinds'])) {
            foreach ($array['businessInsurance']['itemKinds'] as $kindList) {
                $enquiry->VCIPremiumAppend($kindList['basePremium'] ?? '0');

                $code = (string)array_search($kindList['kindCode'], self::$ApiKindList);
                if (!$code) {
                    continue;
                }
                $coverages['code'] = $code;
                $coverages['shortName'] = $kindList['kindName'] ?? '';
                $coverages['insuredDesc'] = (string)($kindList['amount'] ?? '');
                $coverages['total'] = (string)($kindList['actPremium'] ?? '0');

                $enquiry->VCITotalAppend($coverages['total']);
                $enquiry->setVCICoverages($coverages);
            }
        }
        //自主定价系数
        $enquiry->setIndependentPriceRatio($array['businessInsurance']['coefficientInfo']['pricingAdjustValue'] ?? '');
        //无赔款优待优惠系数C01
        $enquiry->setNCDRate($array['businessInsurance']['coefficientInfo']['totalCoefNum'] ?? '');

        $enquiry->setTotal();

        if ($repeat = $array['prpReInsureItems'] ?? []) {
            $enquiry->failure();
            if (count($repeat) >= 1) {
                $enquiry->setErrorTip('商业险重复投保');
            }
        }
        $enquiry->success();

        return $enquiry->get();
    }

    //数据解析
    public function repetition($array): Repetition
    {
        $repetition = new Repetition();

        $repetitionData = $array['result']['businessInsurance']['productInfo']['flatCarReinsuredMessage'] ?? false;

        if ($repetitionData) {
            $repetitionData = explode("\n", trim($repetitionData));

            foreach ($repetitionData as $datum) {
                $value = explode('：', $datum);

                $column = trim($value[0] ?? '');
                $value = trim($value[1] ?? '');
                switch ($column) {
                    case '重复投保保单号':
                        $repetition->setLastPolicy($value);
                        break;
                    case '保险公司代码':
                        $repetition->setLastCompany($value);
                        break;
                    case '号牌号码':
                        $repetition->setLicense($value);
                        break;
                    case '起保日期':
                        $repetition->setLastStartAt($value ? date('Y-m-d H:i:s', strtotime($value)) : '');
                        break;
                    case '终保日期':
                        $repetition->setLastEndTime($value ? date('Y-m-d H:i:s', strtotime($value)) : '');
                        break;
                    case '险种代码':
                        $value = trim(trim($value,')' ),';');
                        $coverage = explode(';', $value);
                        $repetition->setLastCoverage($coverage);
                        break;
                }
            }
        }

        if ($repetition->getLastCompany()) {
            $repetition->setStatus('success');
        }

        return $repetition;
    }
}

